using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;

namespace ProcessesSMS
{
	/// <summary>
	/// Summary description for.
	/// Project name: SMS_xoso
	/// channel: n/a
	/// command code: HN X Y
	/// created: 01/09/09 .
	/// Writer : Nampt + Tuanbc
	/// Company: VIT TELECOM
	/// </summary>

	public class XOSO_MT
	{
		#region Declare
		private SMS_MO WMO;
		private SMS_MT WMT;
		ArrayList arr = new ArrayList();

		public XOSO_MT(SMS_MO MO)
		{
			WMO = MO;
		}

		public XOSO_MT()
		{
		}
		#endregion
		#region Method
		public void run()
		{
			XOSO_WAIT xs_wait = new XOSO_WAIT();
			WMT = new SMS_MT(WMO);
			//            SMS_STR WSTR = new SMS_STR();

			string tmpMsg = WMO.Message.Substring(WMO.CommandCode.Length).Trim().Replace("  ", " ").Replace("  ", " ");
			tmpMsg = tmpMsg.Replace("  ", " ").Trim().ToUpper();
			string[] msg = tmpMsg.Split(new char[] { ' ' });
			string strReturnMsg = "";            
			strReturnMsg = GetReturnMsg(msg);
			//Neu nhan tin CHO
			if(msg.Length==2)
			{
				if(msg[1].ToUpper()=="CHO")
				{
					xs_wait.InsetSMS_WAIT(WMO.MO_ID,WMO.UserID,WMO.ServiceID,WMO.CommandCode,msg[0],WMO.RequestID,WMO.RequestTime,0,1);
					return;
				}
			}
			//Nguoc lai
			if(strReturnMsg!="")
			{
				WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
				WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
				WMT.Insert(); 
			}
			return;

		}        
		private bool ValidInput(string[] tmp)
		{
			if (tmp.Length <= 0 || tmp.Length > 2) return false;
			return true;
		}
		private bool checkIsNumber(string txNumber)
		{
			bool textIsNumeric = true;
			try
			{
				int.Parse(txNumber.Trim());
			}
			catch
			{
				textIsNumeric = false;
			}
			return textIsNumeric;
		}	
		private string GetReturnMsg(string[] arr)
		{	
			#region Khai báo kết nối
			string connString = ConfigurationSettings.AppSettings["STOCK_CONNECTION_STRING"].ToString();
			SqlConnection oledbConn = new SqlConnection(connString);
			string strSQl = string.Empty;
			//SqlCommand cmd = new SqlCommand();
			SqlDataAdapter oleda;// = new SqlDataAdapter();
			DataSet ds;// = new DataSet();
			DataTable dt = new DataTable();
			string code = string.Empty;
			string msgBody = string.Empty;           
			oledbConn.Open();
			#endregion
			/* Khi cú pháp có 3 thành phần. EX: XS DLK 2508 gửi tới 6172*/
			if(arr.Length == 2)
			{	//Nhan ket qua so xo theo ma tinh va theo ngay
				if(checkIsNumber(arr[1]))
				{
					if (WMO.ServiceID.Replace("04","")=="6172")
					{
						#region Trả về kết quả 1 tỉnh theo ngày							
						string d = arr[1].Substring(0,2)  + "/" + arr[1].Substring(2,2) + "/" + DateTime.Now.Year.ToString();									
						strSQl = "SELECT TOP 1 * FROM XOSO WHERE Matinh='" + arr[0]+ "' And Convert(nvarchar(10),ngay,103)='"+ d +"'";								
						//cmd = new SqlCommand(strSQl, oledbConn);
						oleda = new SqlDataAdapter(strSQl, oledbConn);
						//oleda.SelectCommand = cmd;
						ds = new DataSet();
						oleda.Fill(ds, "VIT");
						dt = ds.Tables["VIT"];
						if (dt.Rows.Count>0)
						{
								msgBody = string.Concat(arr[0].ToUpper() +": ",d,"\n",
								"DB:",dt.Rows[0]["GiaiDacBiet"].ToString().Trim().Replace(" ",""),"\n",
								"1:",dt.Rows[0]["GiaiNhat"].ToString().Trim().Replace(" ",""),"\n",
								"2:",dt.Rows[0]["GiaiNhi"].ToString().Trim().Replace(" ",""),"\n",
								"3:",dt.Rows[0]["GiaiBa"].ToString().Trim().Replace(" ",""),"\n",
								"4:",dt.Rows[0]["GiaiTu"].ToString().Trim().Replace(" ",""),"\n",
								"5:",dt.Rows[0]["GiaiNam"].ToString().Trim().Replace(" ",""),"\n",
								"6:",dt.Rows[0]["GiaiSau"].ToString().Trim().Replace(" ",""),"\n",
								"7:",dt.Rows[0]["GiaiBay"].ToString().Trim().Replace(" ",""));	
						}
						else
						{
							msgBody = ConfigSettings.ReadSetting("0Find");
						}
						#endregion
					}
					else
					{
						msgBody = ConfigSettings.ReadSetting("0Find");
					}
				}				
				else//cu phap tin nhan cho nhan ket qua 
				{

				}
			}				
			else//nhan ket qua theo ma tinh
			{				           
				if(WMO.ServiceID.Replace("04","")=="6072")
				{
					msgBody = ConfigSettings.ReadSetting("SMS_HELP");	
				}
				else if(WMO.ServiceID.Replace("04","")=="6172")					
				{					
					strSQl = "SELECT TOP 1 * FROM XOSO WHERE Matinh='" + arr[0]+ "' ORDER BY Ngay DESC";
					//cmd = new SqlCommand(strSQl, oledbConn);
					oleda = new SqlDataAdapter(strSQl, oledbConn);
					//oleda.SelectCommand = cmd;
					ds = new DataSet();
					oleda.Fill(ds, "VIT");
					dt = ds.Tables["VIT"];
					DateTime dateTime = DateTime.Parse(dt.Rows[0]["Ngay"].ToString());
					if (ds.Tables[0].Rows.Count>0)
					{						
						msgBody = string.Concat(arr[0].ToUpper() +":",string.Concat(dateTime.Day,"/",dateTime.Month),"\n",
							"DB:",dt.Rows[0]["GiaiDacBiet"].ToString().Trim().Replace(" ",""),"\n",
							"1:",dt.Rows[0]["GiaiNhat"].ToString().Trim().Replace(" ",""),"\n",
							"2:",dt.Rows[0]["GiaiNhi"].ToString().Trim().Replace(" ",""),"\n",
							"3:",dt.Rows[0]["GiaiBa"].ToString().Trim().Replace(" ",""),"\n",
							"4:",dt.Rows[0]["GiaiTu"].ToString().Trim().Replace(" ",""),"\n",
							"5:",dt.Rows[0]["GiaiNam"].ToString().Trim().Replace(" ",""),"\n",
							"6:",dt.Rows[0]["GiaiSau"].ToString().Trim().Replace(" ",""),"\n",
							"7:",dt.Rows[0]["GiaiBay"].ToString().Trim().Replace(" ",""));
					}
					else
					{
						msgBody = ConfigSettings.ReadSetting("SMS_HELP_MT");
					}
				}	
				else if(WMO.ServiceID.Replace("04","")=="6272")
				{
					msgBody = ConfigSettings.ReadSetting("SMS_HELP");							
				}
				else if(WMO.ServiceID.Replace("04","")=="6372")
				{																					
					msgBody = ConfigSettings.ReadSetting("SMS_HELP");								
				}
				else if(WMO.ServiceID.Replace("04","")=="6472")
				{																					
					msgBody = ConfigSettings.ReadSetting("SMS_HELP");								
				}
				else if(WMO.ServiceID.Replace("04","")=="6572")																						
				{	
					msgBody = ConfigSettings.ReadSetting("SMS_HELP");									
				}
				else if(WMO.ServiceID.Replace("04","")=="6672")
				{																					
					msgBody = ConfigSettings.ReadSetting("SMS_HELP");								
				}
				else if(WMO.ServiceID.Replace("04","")=="6772")					
				{																					
					msgBody = ConfigSettings.ReadSetting("SMS_HELP");								
				}
				else
				{
					msgBody = ConfigSettings.ReadSetting("SMS_HELP");
				}
			}
			oledbConn.Close();
			return msgBody;
		}

		#endregion Method
	}
}
